import logging

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect

from arkaiv.lib.base import BaseController, render
from authkit.authorize.pylons_adaptors import authorize, authorized
from authkit.permissions import HasAuthKitRole, RemoteUser, ValidAuthKitUser, UserIn
import arkaiv.model as model
log = logging.getLogger(__name__)

class ListController(BaseController):
    def index(self):
        if authorized(HasAuthKitRole('admin')):
            c.items = model.getallitems()
            return render('/mainpage.mak')
        if authorized(HasAuthKitRole('internal')):
            c.collections = model.getcollectionswithrole('internal')
            publiccollections = model.getcollectionswithrole('public')
            c.collections = c.collections + publiccollections
        else:
            c.collections = model.getcollectionswithrole('public')
        c.items = [] 
        allitems = []
        for n in c.collections:

            # A collection could be empty, and an empty list element causes 
            # trouble
            items = model.getitemsincollection(n['index'])
            if (not items == []):
                c.items = c.items + items
            
        return render('/mainpage.mak')
             

    def signout(self):
        return "Successfully signed out!"


